﻿// <copyright file="CommunityCommentsByUserId.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-4-13</date>
// <summary>CommunityCommentsbyUserId Silverlight CLR class definition</summary>

namespace NYTimes
{
    using System.ComponentModel;
    using System.Globalization;
    using System.Text;
    using NYTimes.Core;

    /// <summary>
    /// New York Times Community Comments by User Id API
    /// </summary>
    /// <remarks>
    /// <list type="table">
    /// <listheader>
    /// <term>Application Resource Key</term>
    /// <description>Key to add to application resources</description>
    /// </listheader>
    /// <item>
    /// <term>
    /// NYTCommunityKey
    /// </term>
    /// <description>Community API Key</description>
    /// </item>
    /// <item>
    /// <term>
    /// NYTCommunityQPS
    /// </term>
    /// <description>Rate Limiting (default 30 QPS)</description>
    /// </item>
    /// </list>
    /// </remarks>
    public class CommunityCommentsByUserId : CommunityBase
    {
        #region Fields
        /// <summary>
        /// the offset
        /// </summary>
        private int offset;

        /// <summary>
        /// the sort order
        /// </summary>
        private CommentSortType sort;

        /// <summary>
        /// the user ID to query for
        /// </summary>
        private int userId;
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the CommunityCommentsByUserId class
        /// </summary>
        public CommunityCommentsByUserId()
        {
            this.MaxResults = 100;
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the User id
        /// </summary>
        [Description("The UserId for the comment author.")]
        public int UserId 
        {
            get
            {
                return this.userId;
            }

            set
            {
                if (this.userId != value)
                {
                    this.userId = value;

                    this.Offset = 0;
                }
            }
        }

        /// <summary>
        /// Gets or sets the Sort
        /// </summary>
        [Description("The sort order for the comment results.")]
        public CommentSortType Sort
        {
            get
            {
                return this.sort;
            }

            set
            {
                if (this.sort != value)
                {
                    this.sort = value;

                    this.Offset = 0;
                }
            }
        }

        /// <summary>
        /// Gets or sets the maximum number of results to return
        /// </summary>
        [Description("The maximum number of results to return.")]
        public int MaxResults { get; set; }

        /// <summary>
        /// Gets the offset
        /// </summary>
        public int Offset
        {
            get
            {
                return this.offset;
            }

            private set
            {
                this.offset = value;

                this.FetchResults(this.offset == 0);
            }
        }
        #endregion

        #region Methods

        /// <summary>
        /// Generates the URI string
        /// </summary>
        /// <returns>the modified URI string</returns>
        public override string GenerateUriFragment()
        {
            var builder = new StringBuilder();

            builder.AppendFormat(CultureInfo.InvariantCulture, "user/id/{0}.xml?", this.UserId);

            AppendSortType(builder, this.Sort);

            if (this.Offset > 0)
            {
                builder.AppendFormat(CultureInfo.InvariantCulture, "&offset={0}", this.Offset);
            }

            return builder.ToString();
        }

        /// <summary>
        /// Fetch the next set
        /// </summary>
        /// <returns>true if the results are done loading, false otherwise.</returns>
        public override bool FetchNextSet()
        {
            if (this.Results.Count < this.MaxResults && this.Results.Count < this.TotalComments)
            {
                this.Offset += 25;

                return false;
            }

            return true;
        }
        #endregion
    }
}
